<?php

namespace App\Modules\Admin\Services;

use App\Exceptions\Exception;
use App\Models\UploadFile;

class FileService extends BaseService
{
    public function __construct(UploadFile $uploadFile)
    {
        $this->model = $uploadFile;
    }

    public function lists(array $params): array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            if (!empty($search = $request->input('search', ''))){
                $query->where('file_name', 'LIKE', '%' . trim($search) . '%');
            }
            // 分组筛选
            $group_id = intval($request->input('group_id', -1));
            if ($group_id > 0){
                $query->where('group_id', $group_id);
            }
        };
        $params['order'] = 'file_id';
        $params['order_sort'] = 'DESC';
        return parent::lists($params); // TODO: Change the autogenerated stub
    }

    public function removeFileGroup(array $params)
    {
        if ($this->model->whereIn('file_id', $params['file_ids'])->update(['group_id' => $params['group_id']])){
            $this->error = '移动成功！';
            return true;
        }else{
            throw new Exception('移动失败！');
        }
    }
}
